package nongchang.kuaijiecaifu.com.ncapp_android.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.widget.DatePicker;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created by Administrator on 2015/11/18.
 */
public class MathUtil {
    public static ProgressDialog m_pDialog;

    public static String floatUtil(double num) {
        DecimalFormat b = new DecimalFormat("0.00");
        String a = b.format(num);
        return a;
    }

    public static String[] calculate(String type, double principal, double rate, double duetime, boolean selectDay) {
        double interest, sum, every_period;
        int profit_type = 0;
        if (type.equals("每月付息")) {
            if (selectDay) {
                profit_type = -1;
            } else {
                profit_type = 0;
            }
        } else if (type.equals("等本等息")) {
            if (selectDay) {
                profit_type = 2;
            } else {
                profit_type = 1;
            }
        }
        switch (profit_type) {
            case -1:
                String[] profit_type0_day = new String[3];
                interest = (principal * (rate / 12 / 30 / 100) * duetime);
                sum = (principal + interest);
                every_period = (interest / duetime);
                profit_type0_day[0] = floatUtil(sum);
                profit_type0_day[1] = floatUtil(interest);
                profit_type0_day[2] = floatUtil(every_period);
                return profit_type0_day;
            case 0:
                String[] profit_type0_month = new String[3];
                interest = (principal * (rate / 12 / 100) * duetime);
                sum = (principal + interest);
                every_period = (interest / duetime);
                profit_type0_month[0] = floatUtil(sum);
                profit_type0_month[1] = floatUtil(interest);
                profit_type0_month[2] = floatUtil(every_period);
                return profit_type0_month;
            case 1:
                String[] profit_type1_month = new String[3];
                interest = (principal * (rate / 12 / 100) * duetime);
                sum = (principal + interest);
                every_period = (sum / duetime);
                profit_type1_month[0] = floatUtil(sum);
                profit_type1_month[1] = floatUtil(interest);
                profit_type1_month[2] = floatUtil(every_period);
                return profit_type1_month;
            case 2:
                String[] profit_type1_day = new String[3];
                interest = (principal * (rate / 12 / 30 / 100) * duetime);
                sum = (principal + interest);
                every_period = (sum / duetime);
                profit_type1_day[0] = floatUtil(sum);
                profit_type1_day[1] = floatUtil(interest);
                profit_type1_day[2] = floatUtil(every_period);
                return profit_type1_day;
            default:
                break;
        }
        return null;
    }

    public static boolean floatFormat(String text) {
        String regEx = "^([0-9]+)+(.[0-9]+)?";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }

    public static long dateFormat(String date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dt = simpleDateFormat.parse(date);
            return (dt.getTime()) / 1000;
        } catch (Exception ex) {

        }
        return 0;
    }

    public static void getTime(DatePicker datePicker, EditText text) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                monthOfYear += 1;
                text.setText(year + "-" + monthOfYear + "-" + dayOfMonth);
            }
        });
    }

    public static void showProgressDialog(Context context, String text) {
//创建ProgressDialog对象
        m_pDialog = new ProgressDialog(context);
// 设置进度条风格，风格为圆形，旋转的
        m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        // 设置ProgressDialog 提示信息
        m_pDialog.setMessage(text);
        // 设置ProgressDialog 的进度条是否不明确
        m_pDialog.setIndeterminate(false);
        // 设置ProgressDialog 是否可以按退回按键取消
        m_pDialog.setCancelable(true);
        m_pDialog.show();
    }

    public static void closeProgressDialog() {
        if (m_pDialog != null) {
            m_pDialog.hide();
        }
    }

    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    // 替换、过滤特殊字符
    public static String StringFilter(String str) throws PatternSyntaxException {
        str = str.replaceAll("【", "[").replaceAll("】", "]").replaceAll("！", "!");//替换中文标号
        String regEx = "[『』]"; // 清除掉特殊字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    //打电话的Dialog
    public static void phoneCall_Dialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        //    设置Title的图标

        //    设置Title的内容
        //    设置Content来显示一个信息
        builder.setMessage("您确认要打电话给客服吗？");
        //    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + "4000233188"));
                context.startActivity(phoneIntent);
            }
        });
        //    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        //    显示出该对话框
        builder.show();


    }
}
